<?php
// +---------------------------------------------------------------------------+
// | This file is part of the KnowledgebasePublisher package                   |
// | KnowledgebasePublisher - web based knowledgebase publisher tool           |
// |                                                                           |
// | Author:  Evgeny Leontev <eleontev@gmail.com>                              |
// | Copyright (c) 2005 Evgeny Leontev                                         |
// |                                                                           |
// | For the full copyright and license information, please view the LICENSE   |
// | file that was distributed with this source code.                          |
// +---------------------------------------------------------------------------+

require_once "eleontev/Item/Person.php";


class User extends Person
{
	 
	var $properties = array('id'         =>NULL,
	                        'first_name' =>'',
	                        'middle_name'=>'', 
						    'last_name'  =>'',
						    'email'      =>'',
							'phone'      =>'',
						    'username'   =>'',
						    'password'   =>'',
						    'active'     =>'1'
						   );
						   
	var $hidden = array('id');
		
						   
	var $priv = array();
	var $is_admin = true;
	
	
	// we need it to unset (delete) some properties value
	// so to generate right sql for insert, update or replace 	
	function setPassword(&$value) {
		if($value == 1) { 
			unset($this->properties['password']);// mean not insert in db
		}
	}

	function setPriv($values) {
		$this->priv =& $values;
	}
	
	
	function &getPriv() {
		return $this->priv;
	}
	
	
	// some formating goes here
	function _callBack($property, $val) {
		if($property == 'first_name' || $property == 'last_name' || $property == 'middle_name') {
			$val = ucfirst($val);
		} elseif($property == 'password') {
			$val = MD5($val);
		}
		
		return $val;
	}
	
	
	function validate($values, $manager) {
		
		require_once 'eleontev/Validator.php';
		
		
		$required = array('first_name', 'last_name', 'email', 'username');
		
		if($this->is_admin)              { $required[] = 'priv'; }
		if(@!$values['not_change_pass']) { $required[] = 'password'; }

		
		$v =& new Validator($values);

		// check for required first, return errors
		$v->requiredArray('required_msg', $required);
		if($v->getErrors()) {
			$this->errors =& $v->getErrors();
			return true;
		}
		
		
		// we have such username
		if($manager->isUsernameExists($values['username'],  $values['id'])) {
			$v->setError('username_exists_msg', 'username');
		}
		
		if($manager->isEmailExists($values['email'],  $values['id'])) {
			$v->setError('email_exists_msg', 'email');
		}		
		
		
		$v->regex('email_msg', 'email', 'email');
		
		if(@!$values['not_change_pass']) { 
			$v->between('pass_short_msg', 'password', 6); 
			$v->compare('pass_diff_msg', 'password', 'password_2');
		}
		
		
		if($v->getErrors()) {
			$this->errors =& $v->getErrors();
			return true;
		}
	}
}
?>